<?php
require_once('App-Top.php');
require_once('Cookie-Handler.php'); //// Only Activate in UNProtected Area
////require_once('Authorizer.php'); //// Only Activate in Protected Area
require_once('FEFunctions.php');
//// EventsSearch.php
//// This is the Events Table page for the WOG site.

//// Set Page Title
$page_title = 'Events - Whiff Of Grape';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title><?php echo $page_title; ?></title>
        <style type="text/css" media="screen">@import url(style.css);</style>
        <link type="text/css" href="JQuery/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="JQuery/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="JQuery/jquery-ui-1.7.2.custom.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $(".datepicker-inline").datepicker();
                $(".datefield").datepicker();
            });
        </script>
    </head>

<body>
    <div id="bigWrapper">

        <div id="wrapper">

            <div id="header">
            </div>

            <div id="sidebar">

                <?php
                    require_once('MenuLeft.php');
                ?>

            </div>

            <!-- End of Header & Start Content -->

            <div id="body">

                <div id="entryTitlePage"><?php echo $page_title; ?></div>

                <form id="searchEvents" name="searchEvents" method="GET" action="EventsSearch.php" class="frontEnd02">
                <fieldset>

                    <legend>Search Events Between Dates</legend>

                    <ul>
                        <li>
                            <label title="Start Date" for="startdate">Start Date</label>
                            <input name="startdate" type="text" id="startdate" class="datefield" value="<?php if (isset($_GET['startdate'])) { echo $_GET['startdate']; } else { echo date("m/d/Y", mktime(0,0,0, date("m")-6, date("d"), date("Y")));} ?>"  />
                        </li>

                        <li>
                            <label for="enddate">End Date</label>
                            <input name="enddate" type="text" id="enddate" class="datefield" value="<?php if (isset($_GET['enddate'])) { echo $_GET['enddate']; } else { echo date("m/d/Y", mktime(0,0,0, date("m")+3, date("d"), date("Y")));} ?>"  />
                        </li>
                        <input type="submit" name="SearchEvent" value="Search Event" class="ButtonSubmit"/>
                    </ul>

                </fieldset>
                </form>

                <br />

                <table summary="Event Table" class="TSoft" align="center">

                    <tr>
                        <td class="THeader"><a href="EventsSearch.php?qorder=etitle">Title</a></td>
                        <td class="THeader"><a href="EventsSearch.php?qorder=espkrname">Speaker</a></td>
                        <td class="THeader"><a href="EventsSearch.php?qorder=edate">Date</a></td>
                        <td class="THeader">Details</td>
                    </tr>

                <?php
                require_once(MYSQL);
                require_once(STATEM_MYSQL);
                require_once('FEFunctions.php');
                require_once('ShowEventsFunctions.php');


                $queryOrdering = "E.Date";
                if (isset($_GET['qorder'])) {
                    switch(strtolower($_GET['qorder'])){
                        case 'etitle':
                            $queryOrdering = "E.Title";
                            break;

                        case 'espkrname':
                            $queryOrdering = "E.SpeakerName";
                            break;

                        default:
                            $queryOrdering = "E.Date";
                            break;
                    }
                }

                $startDate = date("m/d/Y", mktime(0,0,0, date("m")-6, date("d"), date("Y")));
                if (isset($_GET['startdate'])) {
                    $startDate = $_GET['startdate'];
                }

                $endDate = date("m/d/Y", mktime(0,0,0, date("m")+3, date("d"), date("Y")));
                if (isset($_GET['enddate'])) {
                    $endDate = $_GET['enddate'];
                }

                $arrayStartDate = explode("/", $startDate); // [0]: Month, [1]: Day . [2]: Year
                $sqlDateStart = $arrayStartDate[2] . '-' . $arrayStartDate[0] . '-' . $arrayStartDate[1];

                $arrayEndDate = explode("/", $endDate); // [0]: Month, [1]: Day . [2]: Year
                $sqlDateEnd = $arrayEndDate[2] . '-' . $arrayEndDate[0] . '-' . $arrayEndDate[1];

                $q = sprintf($Q_EventidTitleSpeakerDate_StartdateEnddateOrdering, $sqlDateStart, $sqlDateEnd, $queryOrdering);
                $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                //Check whether the query was successful or not
                if ($r) {

                    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){

                        echo "<tr>";

                        $controllerDisplay = 0;
                        foreach ($row as $value) {
                            if ($controllerDisplay == 0)
                                $controllerDisplay = 1;
                            else
                                echo "<td class='TBody'>" . $value . "</td>";
                        }
                        $controllerDisplay = 0;
                        
                        
                        echo '<td class="TBody"><a href="EventDetail.php?eventID=' . $row['EventId'] . '">Details</a></td>';
                        echo "</tr>";
                    }
                    mysqli_free_result($r);
                    mysqli_close($dbc);
                } else {
                    // Free DB
                    mysqli_close($dbc);
                    die("Query failed");
                }

                ?>


                </table>

                <br />




            </div> <!-- Closes body -->

            <!-- End of Content & Start Footer -->

            <div id="footer">

                <?php
                //// Includer Footer
                include ('footer.php');
                ?>

            </div>

        </div> <!-- Closes Wrapper -->
    </div> <!-- Closes bigWrapper -->
</body>
</html>
<?php
require_once('App-End.php');
?>

<!-- Designed & Developed By Jose Trujillo (2011) -->